package com.ssh.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.ChangGui;
import com.ssh.entity.HelpSale;
import com.ssh.manager.HelpSaleManager;

@Service
public class HelpSaleManagerImpl implements HelpSaleManager {
	@Resource
	private IDao daoImpl;
	@Override
	public HelpSale saveUpdate(HelpSale helpSale) {
		return daoImpl.saveOrUpdateDomain(helpSale);
	}

	@Override
	public HelpSale geyById(Long id) {
		return daoImpl.getDomain(HelpSale.class, id);
	}

	@Override
	public boolean delete(Long id) {
		try {
			HelpSale h = daoImpl.getDomain(HelpSale.class, id);
			h.setDel(1);
			daoImpl.saveOrUpdateDomain(h);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public void findList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from HelpSale where del!=1";
		String findSql = "from HelpSale where del!=1";
		if(conditionMap.get("employee")!=null && !conditionMap.get("employee").equals("")){
			sql += " and taskProductEmployee.employee.id="+conditionMap.get("employee");
			findSql += " and taskProductEmployee.employee.id="+conditionMap.get("employee");
		}
		if(conditionMap.get("task")!=null && !conditionMap.get("task").equals("")){
			sql += " and taskProductEmployee.taskBrand.task.name like '%"+conditionMap.get("task")+"%'";
			findSql += " and taskProductEmployee.taskBrand.task.name like '%"+conditionMap.get("task")+"%'";
		}
		if(conditionMap.get("market")!=null && !conditionMap.get("market").equals("")){
			sql += " and taskProductEmployee.superMarketProduct.superMarket.id="+conditionMap.get("market");
			findSql += " and taskProductEmployee.superMarketProduct.superMarket.id="+conditionMap.get("market");
		}
		int sum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		
		findSql += " order by createDate "+isDesc;
		List<HelpSale> helpSales = (List<HelpSale>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(helpSales);
	}

}
